import type { Module, ActionContext } from 'vuex'
import type { AxiosResponse } from 'axios'
import type { diceStateType } from './typings'
import {
  tenantDictListGet,
  tenantDictRemovePost,
  tenantSelectGet,
  tenantDictTreeGet,
  tenantDictSubmitPost
} from '~/api/system/dict'

const dict: Module<diceStateType, diceStateType> = {
  namespaced: true,
  state: {
    tenantSeleceOpt: []
  },
  getters: {
    tenantSeleceOpt: (state) => state.tenantSeleceOpt
  },
  mutations: {
    GET_TENANT_SELECE(state, payload) {
      state.tenantSeleceOpt = payload
    }
  },
  actions: {
    // 字典管理-列表
    async TenantDictListGet<S, T>(_: ActionContext<S, T>, params: T) {
      return await tenantDictListGet<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 字典管理-删除
    async TenantDictRemovePost<S, T>(_: ActionContext<S, T>, params: T) {
      return await tenantDictRemovePost<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 字典管理-上级tree
    async TenantDictTreeGet<S, T>(_: ActionContext<S, T>, params: T) {
      return await tenantDictTreeGet<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 字典管理-提交
    async TenantDictSubmitPost<S, T>(_: ActionContext<S, T>, params: T) {
      return await tenantDictSubmitPost<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 字典管理-所属租户
    TenantSelectGet<S, T>({ commit }: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        tenantSelectGet<T>(params)
          .then((res: AxiosResponse) => {
            commit('GET_TENANT_SELECE', res?.data?.data || [])
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    }
  }
}

export default dict
